Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
react-fast-compare
Advanced tools
Fastest deep equal comparison for React. Perfect for shouldComponentUpdate. Also really fast general-purpose deep comparison
The react-fast-compare package is a fast deep equal comparison for React. It is optimized for comparing the props or state of React components, although it can be used to compare any data structures. It is particularly useful in shouldComponentUpdate methods to determine if re-rendering is necessary.
Deep comparison of objects and arrays
This feature allows for deep comparison of objects and arrays to determine if they are structurally identical.
import isEqual from 'react-fast-compare';
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const areEqual = isEqual(obj1, obj2); // true
Comparison within React lifecycle methods
This code sample demonstrates how react-fast-compare can be used within a React component's shouldComponentUpdate lifecycle method to prevent unnecessary re-renders.
import React from 'react';
import isEqual from 'react-fast-compare';
class MyComponent extends React.Component {
shouldComponentUpdate(nextProps) {
return !isEqual(this.props, nextProps);
}
render() {
// Component rendering logic
}
}
Lodash's isEqual method provides deep comparison of objects and arrays, similar to react-fast-compare. However, lodash is a general utility library that includes a wide range of functions, which may result in a larger bundle size if only isEqual is needed.
The deep-equal package is another library that can perform deep equality checks. It is not specifically optimized for React and may not be as performant as react-fast-compare in the context of comparing React props and state.
fast-deep-equal is a package that offers a fast deep equality comparison. It is similar to react-fast-compare but is not specifically tailored for React, although it can be used in any JavaScript environment.
The fastest deep equal comparison for React. Really fast general-purpose deep comparison.
Great forshouldComponentUpdate
. This is a fork of the brilliant
fast-deep-equal with some
extra handling for React.
(Check out the benchmarking details.)
$ yarn add react-fast-compare
# or
$ npm install react-fast-compare
const isEqual = require("react-fast-compare");
// general usage
console.log(isEqual({ foo: "bar" }, { foo: "bar" })); // true
// react usage
class ExpensiveRenderer extends React.Component {
shouldComponentUpdate(nextProps) {
return !isEqual(this.props, nextProps);
}
render() {
// ...
}
}
shouldComponentUpdate
?What's faster than a really fast deep comparison? No deep comparison at all.
—This Readme
Deep checks in React's shouldComponentUpdate
should not be used blindly.
First, see if a
PureComponent
would work for you. If it won't (if you need deep checks), it's wise to make
sure you've correctly indentified the bottleneck in your application by
profiling the performance.
After you've determined that you do need deep equality checks and you've
identified the minimum number of places to apply them, then this library may
be for you! For more information about making your app faster, check out the
Optimizing Performance
section of the React docs.
All tests carried out locally on a MacBook. The absolute values are much less important than the relative differences between packages.
Benchmarking source can be found here. Each "operation" consists of running all relevant tests. The React benchmark uses both the generic tests and the react tests; these runs will be slower simply because there are more tests in each operation.
react-fast-compare x 207,503 ops/sec ±0.54% (92 runs sampled)
fast-deep-equal x 195,006 ops/sec ±0.70% (91 runs sampled)
lodash.isEqual x 43,778 ops/sec ±0.55% (91 runs sampled)
nano-equal x 198,036 ops/sec ±0.37% (95 runs sampled)
shallow-equal-fuzzy x 173,023 ops/sec ±0.59% (95 runs sampled)
fastest: react-fast-compare
react-fast-compare
and fast-deep-equal
should be the same speed for these
tests; any difference is just noise. react-fast-compare
won't be faster than
fast-deep-equal
, because it's based on it.
react-fast-compare x 187,628 ops/sec ±0.58% (93 runs sampled)
fast-deep-equal x 477 ops/sec ±0.55% (91 runs sampled)
lodash.isEqual x 35,100 ops/sec ±0.16% (95 runs sampled)
nano-equal x 468 ops/sec ±0.53% (94 runs sampled)
shallow-equal-fuzzy x 684 ops/sec ±0.43% (92 runs sampled)
fastest: react-fast-compare
Three of these packages cannot handle comparing React elements (which are
circular): fast-deep-equal
, nano-equal
, and shallow-equal-fuzzy
.
$ yarn install
$ yarn run benchmark
react-fast-compare@2.0.0 tracks fast-deep-equal@2.0.1
Please see our contributions guide.
2.0.4 (2018-11-09)
react-native
bug introduced by DOM element checking.FAQs
Fastest deep equal comparison for React. Great for React.memo & shouldComponentUpdate. Also really fast general-purpose deep comparison.
The npm package react-fast-compare receives a total of 8,024,554 weekly downloads. As such, react-fast-compare popularity was classified as popular.
We found that react-fast-compare demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 21 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.